<%@ page contentType="text/html;charset=utf-8"%>
<%@page import="java.sql.*,java.io.*"%>

<%!static final String JDBC_DRIVER_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	static final String DB_URL = "jdbc:sqlserver://127.0.0.1;databaseName=student;";
	static final String USER_ID = "sa";
	static final String USER_PASSWORD = "test123";%>



<%
	try {
		InputStream sImage;

		String a = request.getParameter("file_path");

		Class.forName(JDBC_DRIVER_NAME);
		Connection con = DriverManager.getConnection(DB_URL, USER_ID,
				USER_PASSWORD);

		File imgfile = new File(
				"C:/Users/SF-NT900X4D-5/Desktop/zion.t/03.jpg");
		// 		File imgfile = new File(a);
		FileInputStream fin = new FileInputStream(imgfile);
		PreparedStatement pre = con
				.prepareStatement("insert into image(images) values(?)");
		pre.setBinaryStream(1, fin, (int) imgfile.length());
		pre.executeUpdate();
		System.out.println("Inserting Successfully!");
		pre.close();

		Statement st = con.createStatement();
		ResultSet rs = st
				.executeQuery("select images from image where id=5");
		if (rs.next()) {
			byte[] bytearray = new byte[1048576];
			int size = 0;
			sImage = rs.getBinaryStream(1);
			response.reset();
			response.setContentType("image/jpeg");
			while ((size = sImage.read(bytearray)) != -1) {
				response.getOutputStream().write(bytearray, 0, size);
			}
		}
		rs.close();
		st.close();
		con.close();
	} catch (Exception ex) {
		out.println("error :" + ex);
	}
%>